var app = getApp();
var vp = require('../../resource/js/vp.js');
Page({
  data: {
    currentTab: 1, // 0-未开始  1 进行中-2-已结束
	activeUser:{},
	nodata:true,
    ongoingPlans: [
    ],
    upcomingPlans: [
    ],
    completedPlans: [
    ],
	guidances: [
	  { name: '餐前', value: 'before' },
	  { name: '餐后', value: 'after' },
	  { name: '空腹', value: 'empty' },
	  { name: '随餐服用', value: 'with' },
	  { name: '睡前', value: 'sleep' }
	],
  },
  
  // 切换标签
  switchTab: function(e) {
    this.setData({
      currentTab: e.currentTarget.dataset.index
    });
	let no_data = true;
	if(e.currentTarget.dataset.index == 1){
		if(this.data.ongoingPlans.length > 0){
			no_data = false;
		}
	}else if(e.currentTarget.dataset.index == 0){
		if(this.data.upcomingPlans.length > 0){
			no_data = false;
		}
	}else{
		if(this.data.completedPlans.length > 0){
			no_data = false;
		}
	}
	this.setData({
		nodata:no_data
	})
  },
  
  // 编辑计划
  editPlan: function(e) {
    const planId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/vp_pz/pages/familydoctor/editmedicationplan?id=${planId}`
    });
  },
  
  
  // 添加用药计划
  addPlan: function() {
    let tempId=['81eFQzi8bePe7EvMslK-J8BsLBL-it1dow6AlNOOf6E']; 
	wx.requestSubscribeMessage({
	  tmplIds: tempId,
	  success (res) {
		if (res[tempId] === 'accept') {
		  console.log('用户同意了')  
	  
		}else if(res[tempId] == 'reject') { // 用户拒绝授权
		  wx.showModal({
			title: '温馨提示',
			content: "您已关闭消息推送，如需要消息推送服务，请点击确定跳转设置页面打开授权后再次尝试。",
			success: function(modal) {
				if (modal.confirm) { // 点击确定
					wx.openSetting({ withSubscriptions: true })
				}
			}
		})
		}
	   },
	   fail(err) { 
		 console.log(err)
		 if (err.errCode == '20004') {
		  wx.showModal({
			  title: '温馨提示',
			  content: "您的消息订阅主已关闭，如需复习提醒服务，请点击确定跳转设置页面打开授权后再次尝试。",
			  success: function(modal) {
				  if (modal.confirm) { // 点击确定
					  wx.openSetting({ withSubscriptions: true })
				  }
			  }
		  })
	  }
		},
		complete(res) {
		  console.log('complete  调用完成')
		  // 无论取消还是接收都会执行:比如 下单(无论订阅号是取消还是接收都执行)
		  wx.navigateTo({
			url: '/vp_pz/pages/familydoctor/editmedicationplan'
		  });
		}
	})
  },
  
  // 查看用药提醒
  // viewReminders: function() {
  //   wx.navigateTo({
  //     url: '/pages/reminders/reminders'
  //   });
  // },
  // 加载数据
  loadData() {    
    this.setData({ loading: true });
    var that = this
      app.util.request({
    	  url: 'Medicalmanage/getMedicalPlan',
    	  data: {
    		  'family_id':this.data.activeUser.id
    	  },
    	  success(res) {
    		  var data = res.data.data;
    		  console.log(data)
			  if(data.ongoingPlans.length > 0){
				  that.setData({
					  nodata:false
				  })
			  }
    		  that.setData({
    			  'ongoingPlans':data.ongoingPlans,
    			  'upcomingPlans':data.upcomingPlans,
    			  'completedPlans':data.completedPlans,
  				  'loading': false
    		  })
    	  }
      })
  },
  onShow: function() {
    // 这里可以添加从服务器获取数据的逻辑
  	this.setData({
  		  activeUser:wx.getStorageSync('activeUser')
  	})
  	this.loadData()
  },
  onLoad: function() {
    // 这里可以添加从服务器获取数据的逻辑
	// this.setData({
	// 	  activeUser:wx.getStorageSync('activeUser')
	// })
	// this.loadData()
  }
});